package com.mfin.bs.mgr.impl;

import java.util.List;

import com.mfin.bom.factory.CustomerInformationFactory;
import com.mfin.bom.factory.FinanceInformationFactory;
import com.mfin.bom.factory.LoanMasterFactory;
import com.mfin.bom.factory.interfaces.ICustomerInformationFactory;
import com.mfin.bom.factory.interfaces.IFinanceInformationFactory;
import com.mfin.bom.factory.interfaces.ILoanMasterFactory;
import com.mfin.bom.interfaces.IAddress;
import com.mfin.bom.interfaces.IChitMaster;
import com.mfin.bom.interfaces.ICustomer;
import com.mfin.bom.interfaces.IFinance;
import com.mfin.bom.interfaces.IPartner;
import com.mfin.bom.interfaces.IUser;
import com.mfin.bs.mgr.interfaces.IFinanceInformationManager;
import com.mfin.vo.AddressVO;
import com.mfin.vo.CustomerListVO;
import com.mfin.vo.CustomerVO;
import com.mfin.vo.PartnerListVO;
import com.mfin.vo.PartnerVO;
import com.mfin.vo.UserVO;
import com.mfin.vo.chits.ChitMasterApplicationVO;
import com.mfin.vo.chits.ChitMasterVO;
import com.mfin.vo.finance.FinanceVO;

public class FinanceInformationManager extends BaseManager implements
		IFinanceInformationManager {

	IFinanceInformationFactory financeInformationFactory = new FinanceInformationFactory();
	ILoanMasterFactory loanMasterFactory = new LoanMasterFactory();
	ICustomerInformationFactory customerInformationFactory = new CustomerInformationFactory();

	@Override
	public FinanceVO createFinance(FinanceVO mainBranch) {
		logMethodBegin("FinanceInformationManager", "createMainBranch");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		IFinance bomFinance = financeInformationFactory.createFinance();

		map(mainBranch, bomFinance, false, false);
		map(bomFinance, mainBranch, false, true);

		logMethodEnd("FinanceInformationManager", "createMainBranch");
		return mainBranch;
	}

	@Override
	public FinanceVO getFinance(FinanceVO financeVO) {
		logMethodBegin("FinanceInformationManager", "addCustomer");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		IFinance bomFinance = financeInformationFactory
				.getFinanceInformation(financeVO);

		map(bomFinance, financeVO, false, true);

		logMethodEnd("FinanceInformationManager", "addCustomer");
		return financeVO;
	}

	@Override
	public FinanceVO updateFinance(FinanceVO financeVO) {
		logMethodBegin("FinanceInformationManager", "addCustomer");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		IFinance bomFinance = financeInformationFactory
				.getFinanceInformation(financeVO);

		map(financeVO, bomFinance, false, false);
		map(bomFinance, financeVO, false, true);

		logMethodEnd("FinanceInformationManager", "addCustomer");
		return financeVO;
	}

	@Override
	public CustomerVO addCustomer(FinanceVO finance, CustomerVO customer) {
		logMethodBegin("FinanceInformationManager", "addCustomer");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		IFinance bomFinance = financeInformationFactory
				.getFinanceInformation(finance);
		ICustomer bomCustomer = financeInformationFactory
				.createCustomer(finance);

		map(customer, bomCustomer, false, false);
		AddressVO comAddressVO = customer.getCommunicationAddress();
		if (comAddressVO != null) {
			IAddress bomComAddress = customerInformationFactory.createAddress();
			map(comAddressVO, bomComAddress, false, false);
			bomCustomer.setCommunicationAddress(bomComAddress);
		}

		AddressVO prmtAddressVO = customer.getPermanentAddress();
		if (prmtAddressVO != null) {
			IAddress bomPrmtAddress = customerInformationFactory
					.createAddress();
			map(prmtAddressVO, bomPrmtAddress, false, false);
			bomCustomer.setPermanentAddress(bomPrmtAddress);
		}
		bomCustomer.setFinance(bomFinance);
		map(bomCustomer, customer, false, true);

		logMethodEnd("FinanceInformationManager", "addCustomer");
		return customer;
	}

	@Override
	public PartnerVO addPartner(FinanceVO finance, PartnerVO partnerVO) {
		logMethodBegin("FinanceInformationManager", "addPartner");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		customerInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());

		IFinance bomFinance = financeInformationFactory
				.getFinanceInformation(finance);
		ICustomer bomCustomer = financeInformationFactory
				.createCustomer(finance);
		IPartner bomPartner = financeInformationFactory.createPartner(finance);

		map(partnerVO, bomPartner, false, false);
		if (partnerVO instanceof CustomerVO) {

			CustomerVO customerVO = (CustomerVO) partnerVO;
			map(customerVO, bomCustomer, false, false);

			AddressVO comAddressVO = customerVO.getCommunicationAddress();
			if (comAddressVO != null) {
				IAddress bomComAddress = customerInformationFactory
						.createAddress();
				map(comAddressVO, bomComAddress, false, false);
				bomCustomer.setCommunicationAddress(bomComAddress);
			}

			AddressVO prmtAddressVO = customerVO.getPermanentAddress();
			if (prmtAddressVO != null) {
				IAddress bomPrmtAddress = customerInformationFactory
						.createAddress();
				map(prmtAddressVO, bomPrmtAddress, false, false);
				bomCustomer.setPermanentAddress(bomPrmtAddress);
			}
		}

		bomPartner.setFinance(bomFinance);
		bomCustomer.setFinance(bomFinance);
		bomPartner.setCustomer(bomCustomer);

		map(bomPartner, partnerVO, false, true);
		if (partnerVO instanceof CustomerVO) {
			map(bomPartner.getCustomer(), (CustomerVO) partnerVO, false, false);
		}

		logMethodEnd("FinanceInformationManager", "addPartner");
		return partnerVO;
	}

	@Override
	public UserVO createUser(FinanceVO finance, UserVO userVO) {
		logMethodBegin("FinanceInformationManager", "createUser");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		IFinance bomFinance = financeInformationFactory
				.getFinanceInformation(finance);
		IUser bomUser = financeInformationFactory.createUser(finance);

		map(userVO, bomUser, false, false);
		bomUser.setFinance(bomFinance);
		map(bomUser, userVO, false, true);

		logMethodEnd("FinanceInformationManager", "createUser");
		return userVO;
	}

	@Override
	public FinanceVO getFinance(UserVO userVO) {
		logMethodBegin("FinanceInformationManager", "addCustomer");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		IFinance bomFinance = financeInformationFactory
				.getFinanceInformation(userVO);

		FinanceVO financeVO = new FinanceVO();
		map(bomFinance, financeVO, false, true);

		logMethodEnd("FinanceInformationManager", "addCustomer");
		return financeVO;
	}

	@Override
	public CustomerListVO getFinanceCustomers(FinanceVO financeVO) {
		logMethodBegin("FinanceInformationManager", "getAllCustomers");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		IFinance bomFinance = financeInformationFactory
				.getFinanceInformation(financeVO);

		List<ICustomer> bomCustomerList = bomFinance.getCustomers();

		CustomerListVO customerListVO = null;

		if (bomCustomerList != null) {
			CustomerVO[] customerArray = new CustomerVO[bomCustomerList.size()];

			for (int index = 0; index < bomCustomerList.size(); index++) {
				map(bomCustomerList.get(index), customerArray[index], false,
						true);
			}

			customerListVO = new CustomerListVO(customerArray);
		}

		logMethodEnd("FinanceInformationManager", "getAllCustomers");
		return customerListVO;
	}

	@Override
	public PartnerListVO getFinancePartners(FinanceVO financeVO) {
		logMethodBegin("FinanceInformationManager", "getFinancePartners");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		IFinance bomFinance = financeInformationFactory
				.getFinanceInformation(financeVO);

		List<IPartner> bomPartnerList = bomFinance.getPartners();

		PartnerListVO partnerListVO = null;

		if (bomPartnerList != null) {
			PartnerVO[] partnerArray = new PartnerVO[bomPartnerList.size()];

			for (int index = 0; index < bomPartnerList.size(); index++) {
				map(bomPartnerList.get(index), partnerArray[index], false, true);
				if (partnerArray[index] instanceof CustomerVO) {
					map(bomPartnerList.get(index).getCustomer(),
							(CustomerVO) partnerArray[index], false, false);
				}
			}

			partnerListVO = new PartnerListVO(partnerArray);
		}

		logMethodEnd("FinanceInformationManager", "getFinancePartners");
		return partnerListVO;
	}

	@Override
	public ChitMasterVO createChitMaster(FinanceVO financeVO,
			ChitMasterApplicationVO chitMasterApplicationVO) {
		logMethodBegin("FinanceInformationManager", "createChitMaster");

		financeInformationFactory
				.setPersistenceGovernor(getPersistenceGovernor());
		IFinance bomFinance = financeInformationFactory
				.getFinanceInformation(financeVO);

		IChitMaster bomChitMaster = financeInformationFactory
				.createChitMaster(bomFinance);

		ChitMasterVO chitMasterVO = new ChitMasterVO();
		map(chitMasterApplicationVO, bomChitMaster, false, false);
		map(bomChitMaster, chitMasterVO, false, true);
		logMethodEnd("FinanceInformationManager", "createChitMaster");
		return chitMasterVO;
	}
}